/**
 * 
 */
package dms.presentation.action.supervisor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.MappingDispatchAction;

import dms.dto.DormSupervisorDTO;
import dms.model.form.PurchaseItem;
import dms.model.form.Repair;
import dms.model.role.DormSupervisor;
import dms.model.role.wrapper.DormSupervisorWrapper;
import dms.model.util.PageIterator;
import dms.service.PurchaseItemQueryService;
import dms.service.RepairQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;

/**
 * @author 李健
 *
 */
public class InformationAction extends MappingDispatchAction {
	private static final Log log = LogFactory.getLog(InformationAction.class);
	/**
	 * 提取个人信息，放在request作用域
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward personal(ActionMapping mapping, 
								  ActionForm form,
								  HttpServletRequest request,
								  HttpServletResponse response) throws Exception {
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = wrapper.getDormSupervisor();
		
		request.setAttribute("dormSupervisor", dormSupervisor);
		return mapping.findForward(Constant.SUCCESS);
	}

	/**
	 * 修改个人信息不包括密码
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward modifyInfo(ActionMapping mapping, 
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) throws Exception {
		
		ActionErrors errors= new ActionErrors();
		DormSupervisorDTO dto = (DormSupervisorDTO) form;
		
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		dto.setId(wrapper.getDormSupervisor().getId());
		
		try {
			wrapper.modifyPersonalInfo(dto);
		} catch (Exception e) {
			this.log.error(e.getMessage());
			return mapping.findForward(Constant.ERROR);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward modifyInfoForm(ActionMapping mapping, 
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) throws Exception {
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = wrapper.getDormSupervisor();
		
		request.setAttribute("dormSupervisor", dormSupervisor);
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward modifyPassword(ActionMapping mapping, 
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) throws Exception {
		
		DormSupervisorDTO dto = (DormSupervisorDTO) form;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		dto.setId(wrapper.getDormSupervisor().getId());
		try {
			wrapper.modifyPersonalInfo(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.ERROR);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward mainFrame(ActionMapping mapping, 
			  ActionForm form,
			  HttpServletRequest request,
			  HttpServletResponse response) throws Exception {
		 PurchaseItemQueryService purchaseItemQueryService = (PurchaseItemQueryService) ContextUtil.getContext().getBean("purchaseItemQueryService");
		 RepairQueryService repairQueryService = (RepairQueryService) ContextUtil.getContext().getBean("repairQueryService");
		 
		 PageIterator<PurchaseItem> purchaseItems = purchaseItemQueryService.findByBuilding(0, 0, getCurrentDormSupervisor().getBuilding(), null, null, false, false);
		 PageIterator<Repair> repairs = repairQueryService.findByBuilding(0, 0, null, null, getCurrentDormSupervisor().getBuilding(), false, false);
		 
		 DormSupervisorDTO dto = (DormSupervisorDTO) form;
		 dto.setPurchaseCount(purchaseItems.getAllCount().toString());
		 dto.setRepairCount(repairs.getAllCount().toString());
		 
		 return mapping.findForward(Constant.SUCCESS);
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return this.personal(mapping, form, request, response);
	}
	
	private DormSupervisor getCurrentDormSupervisor() {
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		return dormSupervisorWrapper.getDormSupervisor();
	}
}







